package com.ruoyi.durian.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.durian.domain.LlMap;
import com.ruoyi.durian.domain.LlOrder;
import com.ruoyi.durian.domain.LlSearchHistory;
import com.ruoyi.durian.domain.dto.SearchHistoryDto;
import com.ruoyi.durian.mapper.LlMapMapper;
import com.ruoyi.durian.mapper.LlOrderMapper;
import com.ruoyi.durian.service.LlMapService;
import com.ruoyi.durian.service.LlSearchHistoryService;
import com.ruoyi.durian.mapper.LlSearchHistoryMapper;
import org.apache.xmlbeans.impl.xb.xsdschema.NamespaceList;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Date;
import java.util.List;

/**
 * @author lenovo
 * @description 针对表【ll_search_history(订单模块-搜索历史记录)】的数据库操作Service实现
 * @createDate 2024-07-26 16:17:15
 */
@Service
public class LlSearchHistoryServiceImpl extends ServiceImpl<LlSearchHistoryMapper, LlSearchHistory>
        implements LlSearchHistoryService {

    @Resource
    private LlSearchHistoryMapper llSearchHistoryMapper;

    @Resource
    private LlMapService llMapService;

    @Resource
    private LlMapMapper llMapMapper;

    /**
     * 历史搜索记录
     *
     * @return
     */
    @Override
    public List<SearchHistoryDto> searchHistory() {
        Integer userId = SecurityUtils.getUserId().intValue();
        return llSearchHistoryMapper.selectsearchHistory(userId);
    }

    /**
     * 搜索
     *
     * @param name
     * @return
     */
    @Override
    public List<LlMap> searchLike(String name) {
        LambdaQueryWrapper<LlMap> wrapper = new LambdaQueryWrapper<>();
        wrapper.like(LlMap::getName, name)
                .or(wrapper1 -> wrapper1.like(LlMap::getAddress, name));
        List<LlMap> list = llMapService.list(wrapper);
        return list;
    }

    /**
     * 添加历史搜索记录
     *
     * @return
     */
    @Override
    @Transactional
    public AjaxResult addHistory(SearchHistoryDto searchHistoryDto) {

        Integer userId = SecurityUtils.getUserId().intValue();

        LlSearchHistory history = new LlSearchHistory();
        history.setIsDelete(0);
        history.setCreateTime(new Date());
        history.setUpdateTime(new Date());
        history.setUserId(userId);
        LlMap llMap = llMapMapper.selectById(searchHistoryDto.getMapId());
        history.setMapId(llMap.getId());
        history.setAddress(llMap.getName());
        history.setAllAddress(llMap.getAddress());

        llSearchHistoryMapper.insert(history);

        return AjaxResult.success();
    }

    /**
     * 历史搜索记录
     *
     * @return
     */
    @Override
    public List<SearchHistoryDto> selectsearchHistoryByAddressName(String name) {
        LambdaQueryWrapper<LlMap> qw = new LambdaQueryWrapper<>();
        qw.like(LlMap::getName, name);
        List<LlMap> mapList = llMapMapper.selectList(qw);
        List<SearchHistoryDto> list = new ArrayList<>();
        mapList.forEach(item -> {
            SearchHistoryDto dto = new SearchHistoryDto();
            dto.setMapId(item.getId());
            dto.setAddress(item.getName());
            dto.setAllAddress(item.getAddress());
            list.add(dto);
        });
        return list;
    }

    /**
     * 删除历史记录
     *
     * @return
     */
    @Override
    @Transactional
    public AjaxResult deleteHistory() {
        Integer userId = SecurityUtils.getUserId().intValue();
        LambdaQueryWrapper<LlSearchHistory> qw = new LambdaQueryWrapper<>();
        qw.eq(LlSearchHistory::getUserId,userId);
        llSearchHistoryMapper.delete(qw);

        return AjaxResult.success("清空成功！！！");
    }

}




